<template>
  <div>
    <div>
      <el-input v-model="jobLevel.name" placeholder="添加职称等级..." prefix-icon="el-icon-plus"
      style="width: 300px" size="small"></el-input>
      <el-select v-model="jobLevel.titleLevel" placeholder="职称等级" size="small" style="margin-left: 6px;margin-right: 6px">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addJobLevel">添加</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table
          :data="jobLevels"
          stripe
          border
          size="small"
          style="width: 100%">
        <el-table-column
            prop="id"
            label="编号"
            width="70">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级"
            width="150">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建日期"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="150">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :pager-count="5"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "JobLevelMenu",
  data(){
    return{
      jobLevel: {
        name: '',
        titleLevel: ''
      },
      titleLevels: [
          '正高级',
          '副高级',
          '中级',
          '初级',
          '员级'
      ],
      jobLevels: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  mounted() {
    this.initJobLevel();
  },
  methods: {
    handleDelete(data){
      this.$confirm('此操作将永久删除['+data.name+']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/jobLevel/delete/'+data.id).then(resp=>{
          if (resp){
            this.initJobLevel();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addJobLevel(){
      if (this.jobLevel.name && this.jobLevel.titleLevel){
        this.postRequest('/system/basic/jobLevel/add',this.jobLevel).then(resp=>{
          if (resp){
            this.initJobLevel();
          }
        })
      }else {
        this.$message.error('字段不能为空');
      }
    },
    initJobLevel(){
      const params = {
        currentPage: this.jobLevels.currentPage,
        pageSize: this.jobLevels.pageSize,
      };
      this.postRequest('/system/basic/jobLevel/list',params).then(resp=>{
        if (resp){
          this.pagination.total = resp.data.total;
          this.jobLevels = resp.data.list;
          this.jobLevel.name = '';
          this.jobLevel.titleLevel = '';
        }
      })
    },
    handleSizeChange(val){
      this.pageSize = val;
    },
    handleCurrentChange(val){
      this.currentPage = val;
    }
  }
}
</script>

<style>
.block{
  margin-top: 20px;
  text-align: center;
}
</style>
